namespace :db do
  namespace :version do
    desc "Manually set the schema version to an arbitrary number, used to recover from failed migration states. VERSION to be given"
    task :set => :environment do
     # eval "#{model_or_raise}.to_fixture(#{limit_or_nil_string})"
        if ENV['VERSION'].blank?
          raise "No version supplied Current Version: #{ActiveRecord::Migrator.current_version}. Set VERSION to change version."
        else
          puts "Current Version: #{ActiveRecord::Migrator.current_version}\r\n"
          ActiveRecord::Migrator.set_schema_version(ENV['VERSION'])
          puts "New version: #{ActiveRecord::Migrator.current_version}"          
        end
    end
  end
  
  namespace :migrate do
    desc "Like db:migrate except it automatically targets the next version up"
    task :up => :environment do
      ActiveRecord::Migrator.migrate("db/migrate/",ActiveRecord::Migrator.current_version.to_i + 1 )
      puts "New version: #{ActiveRecord::Migrator.current_version}"          
    end
    
    desc "Like db:migrate except it automatically targets the next version down"
    task :down => :environment do
      ActiveRecord::Migrator.migrate("db/migrate/",ActiveRecord::Migrator.current_version.to_i - 1 ) unless ActiveRecord::Migrator.current_version == 0
      puts "New version: #{ActiveRecord::Migrator.current_version}"          
    end
  end
end